<?php

class AuthController extends Zend_Controller_Action
{
    protected $_redirector;

    public function init()
    {
        $this->_redirector = $this->_helper->getHelper('Redirector');

        @session_start();
    }

    public function loginAction()
    {
        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            // get user
            $user_db = new Backend_Model_DbTable_User();
            $user = $user_db->getUserByEmail($postData['email']);

            // Check exsistence
            if(!$user){
                $error = array('message'=>'Wrong email/password','prev'=>'/index');
                $_SESSION['error'] = $error;
                $this->_redirector->gotoUrl('/error/displayerror');
            }

            //Check password
            if(strcmp($user['password'], sha1($postData['password'])) != 0){
                $error = array('message'=>'Wrong email/password','prev'=>'/index');
                $_SESSION['error'] = $error;
                $this->_redirector->gotoUrl('/error/displayerror');
            }

            $_SESSION['user'] = $user;
        }

        $this->_redirector->gotoUrl('/');
    }

    public function logoutAction()
    {
        if(isset($_SESSION['user'])){
            unset ($_SESSION['user']);
        }

        $this->_redirector->gotoUrl('/');
    }


}





